package com.wnxy.sddfp.system.config;

import com.netflix.loadbalancer.IRule;
import com.netflix.loadbalancer.RandomRule;
import com.netflix.loadbalancer.RoundRobinRule;
import com.wnxy.sddfp.common.util.JwtTemplate;
import feign.Logger;
import org.hibernate.validator.HibernateValidator;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.client.RestTemplate;

import javax.validation.Validation;
import javax.validation.Validator;

@Configuration
public class SystemConfiguration {

    /**
     * SpringSecurity的加密工具类配置
     * @return
     */
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    /**
     * SpringBoot参数校验的快速失败配置; 作用：提高性能；
     * @return
     */
    @Bean
    public Validator validator() {
        return Validation.byProvider(HibernateValidator.class)
                .configure() // 开始配置
                .failFast(true) // 配置快速失败
                .buildValidatorFactory() // 构建Validator工厂
                .getValidator(); // 从Validator工厂中获取Validator对象
    }

    /**
     * 创建JwtTemplate工具类
     * @return
     */
    @Bean
    public JwtTemplate jwtTemplate(){
        return new JwtTemplate();
    }

    /**
     * 创建远程调用工具类
     */
    @Bean
    public RestTemplate restTemplate(){
        return new RestTemplate();
    }

    /**
     * 配置MQ消息的转换器（json转换为对象）
     */
    @Bean
    public MessageConverter messageConverter(){
        return new Jackson2JsonMessageConverter();
    }

    /**
     * 开启feign的通讯日志
     * @return
     */
    @Bean
    public Logger.Level loggerLevel(){
        return Logger.Level.FULL;
    }

    //负载均衡全局配置：轮询
    @Bean
    public IRule rules(){
        return new RoundRobinRule();
    }
}